﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Diagnostics;


namespace ControllerClient.UserTool
{
    class EpcReader
    {
        static LogHelper log = new LogHelper();
        //3B9D8CAA1100002E00DD3E45
        public static void test()
        {
           //string returnValue= readEpc(strToToHexByte("4786C21C114E205200374C01"),"1");
            //LogHelper.Debug("returnValue" + returnValue);
            //481F2281114E208C010E7D00
            //3B9D8CAA1200002E00DD3E45 真实EPC
            //3B9CBC3C110000AD00589800

        }
        public static bool checkEpc(byte[] epcbyte)
        {
            
            
                TagEpcData ted = new TagEpcData();
                BitArray myBA = new BitArray(epcbyte);
                BitArray tagidBa = new BitArray(32);


                for (int i = 0; i < 32; i++)//0
                {
                    tagidBa[i] = myBA[i];
                }
                byte[] tagidBaBytes = new byte[4];
               
                tagidBa.CopyTo(tagidBaBytes, 0);
                Array.Reverse(tagidBaBytes);
                ted.Tagid = BitConverter.ToInt32(tagidBaBytes, 0);
                string epc = ted.Tagid.ToString();
                BitArray versionidBA = new BitArray(4);
                for (int i = 0; i < 4; i++)//32
                {
                    versionidBA[i] = myBA[i + 36];
                }
                byte[] versionidBABytes = new byte[4];
                versionidBA.CopyTo(versionidBABytes, 0);
                // Array.Reverse(versionidBABytes);
                int versionId = BitConverter.ToInt32(versionidBABytes, 0);
                Console.WriteLine("包号为"+ epc+"版本号为:" + versionId);
                
                    return true;
                
               
            
        }

        public static TagEpcData readEpc(byte[] epcbyte)
        {
            //LogHelper.Debug("EPC数据为:" + ToHexString(epcbyte, epcbyte.Length));
            string epcAll = "";
            TagEpcData ted = new TagEpcData();
            try
            {
                
                BitArray myBA = new BitArray(epcbyte);
                BitArray tagidBa = new BitArray(32);
               

                for (int i = 0; i < 32; i++)//0
                {
                    tagidBa[i] = myBA[i];
                }
                byte[] tagidBaBytes = new byte[4];
                //mybyte[0] = 0xAA;
                //mybyte[1] = 0x8c;
                //mybyte[2] = 0x9D;
                //mybyte[3] = 0x3b;
                tagidBa.CopyTo(tagidBaBytes, 0);
                Array.Reverse(tagidBaBytes);
                Console.WriteLine("包号为:" + BitConverter.ToInt32(tagidBaBytes, 0));
                ted.Tagid = BitConverter.ToInt32(tagidBaBytes, 0);
                epcAll = ted.Tagid.ToString();
                BitArray versionidBA = new BitArray(4);
                for (int i = 0; i < 4; i++)//32
                {
                    versionidBA[i] = myBA[i + 36];
                }
                byte[] versionidBABytes = new byte[4];
                versionidBA.CopyTo(versionidBABytes, 0);
                // Array.Reverse(versionidBABytes);
                Console.WriteLine("版本号为:" + BitConverter.ToInt32(versionidBABytes, 0));
                ted.Versionid = BitConverter.ToInt32(versionidBABytes, 0);
                    BitArray pervalueidBA = new BitArray(4);
                    for (int i = 0; i < 4; i++)//36
                    {
                        pervalueidBA[i] = myBA[i + 32];
                    }
                    byte[] pervalueidBABytes = new byte[4];
                    pervalueidBA.CopyTo(pervalueidBABytes, 0);
                // Array.Reverse(pervalueidBABytes);
                Console.WriteLine("券别为:" + BitConverter.ToInt32(pervalueidBABytes, 0));
                    ted.Pervalueid = BitConverter.ToInt32(pervalueidBABytes, 0);

                    BitArray amountBA = new BitArray(16);
                    for (int i = 0; i < 16; i++)//40
                    {
                        amountBA[i] = myBA[i + 40];
                    }
                    byte[] amountBABytes = new byte[2];
                    amountBA.CopyTo(amountBABytes, 0);
                    Array.Reverse(amountBABytes);
                    Console.WriteLine("张数为:" + BitConverter.ToInt16(amountBABytes, 0));
                    ted.Amount = BitConverter.ToInt16(amountBABytes, 0);

                    BitArray randomBA = new BitArray(8);
                    for (int i = 0; i < 8; i++)//56
                    {
                        randomBA[i] = myBA[i + 56];
                    }
                    byte[] randomBABytes = new byte[2];
                    randomBA.CopyTo(randomBABytes, 0);
                    Array.Reverse(randomBABytes);
                    Console.WriteLine("随机数为:" + BitConverter.ToInt16(randomBABytes, 0));
                    ted.Random = BitConverter.ToInt16(randomBABytes, 0).ToString();


                    BitArray opAndcheckBA = new BitArray(24);
                    for (int i = 0; i < 24; i++)
                    {
                        opAndcheckBA[i] = myBA[i + 64];
                    }
                    byte[] opAndcheckBytes = new byte[3];
                    opAndcheckBA.CopyTo(opAndcheckBytes, 0);
                    //Array.Reverse(opAndcheckBytes);
                    Console.WriteLine("opAndcheckBytes=" + ToHexString(opAndcheckBytes, opAndcheckBytes.Length));
                    string operatecountstr = "0" + ToHexString(opAndcheckBytes, opAndcheckBytes.Length).Substring(0, 3);
                    byte[] operatecountbytes = strToToHexByte(operatecountstr);
                    Array.Reverse(operatecountbytes);
                    int operatecount = BitConverter.ToInt16(operatecountbytes, 0);
                    Console.WriteLine("操作数为:" + operatecount);
                    ted.Operatecount = operatecount;

                    string checkcode = ToHexString(opAndcheckBytes, opAndcheckBytes.Length).Substring(3, 3);
                    Console.WriteLine("校验码为:" + checkcode);
                    ted.Checkcode = checkcode;




                    BitArray stutsBa = new BitArray(8);

                    for (int i = 0; i < 8; i++)//88
                    {
                        stutsBa[i] = myBA[i + 88];

                    }

                    Debug.WriteLine(stutsBa[0] == false ? "开锁" : "关锁");
                    ted.Lockstuts = stutsBa[0];
                    Debug.WriteLine(stutsBa[2] || stutsBa[1] == false ? "正常" : "异常");
                    ted.LockeEx = stutsBa[2] || stutsBa[1];
                    Debug.WriteLine(stutsBa[3] == false ? "EPC正常" : "EPC异常");
                    ted.EpcEx = stutsBa[3];
                    Debug.WriteLine(stutsBa[6] == false ? "未上电" : "上电");
                    ted.HasElec = stutsBa[6];
                    Debug.WriteLine(stutsBa[7] == false ? "工作完成/工作未开始" : "工作中");
                    ted.Jobstuts = stutsBa[7];
                    if (ted.Lockstuts == false)
                    {
                        //开锁
                        epcAll = epcAll + "2";
                    }
                    else
                    {
                        epcAll = epcAll + "3";
                    }
                    //if (ted.Jobstuts == false)
                    //{
                    //    //工作完成/工作未开始
                    //    epcAll = epcAll + "0";
                    //}
                    //else
                    //{
                    //    epcAll = epcAll + "1";
                    //}
                    if (ted.LockeEx == false)
                    {
                        //正常
                        epcAll = epcAll + "4";
                    }
                    else
                    {
                        epcAll = epcAll + "5";
                    }
                    epcAll = epcAll + "0" + ted.Pervalueid.ToString();
                if (epcAll.Length < 13) {
                    epcAll = "0000000000000";
                    
                }

                //}

            }
            catch (Exception e)
            {
                LogHelper.Debug("标签解析失败" + e);
            }

            return ted;
        }
        //private string checkCode(byte[] epcbytes,byte[] tid)
        //{

        //    DESEncrypt des = new DESEncrypt();
        //    des.Encrypt("1B9DADFBF92E002F",);
        //    //byte[] Abyte = new byte[8];
        //    //for (int i = 0; i < 8; i++)
        //    //{
        //    //    Abyte[i] = epcbytes[i];
        //    //}
        //    //byte[] Bbyte = new byte[8];
        //    //Bbyte[0] = epcbytes[8];
        //    //Bbyte[1] = (byte)epcbytes[9]>>4;
        
        //}
       private static byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString = hexString.Insert(hexString.Length - 1, 0.ToString());
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
       public static string ToHexString(byte[] bytes, int lent) // 0xae00cf => "AE00CF "
       {
           string hexString = string.Empty;
           if (bytes != null)
           {
               StringBuilder strB = new StringBuilder();

               for (int i = 0; i < lent; i++)
               {
                   strB.Append(bytes[i].ToString("X2"));
               }
               hexString = strB.ToString();
           }
           return hexString;
       }

               


    }
}
